import datetime

from django.core.exceptions import ValidationError
from django.db import models


class Message(models.Model):
    def __str__(self):
        if self.image:
            return f"{self.username}:{self.image.url}"

        return f"{self.username}:{self.content}"

    username = models.CharField(max_length=255, verbose_name="用户名")
    room = models.CharField(max_length=255, verbose_name="聊天室")
    content = models.TextField(verbose_name="内容")
    create_time = models.DateTimeField(verbose_name="写入时间", auto_now=True)
    image = models.ImageField(blank=True, null=True, verbose_name='图片')  # 存储图片路径

    def clean(self):
        if self.image and self.content:
            raise ValidationError("一条消息不能同时包含图片和文字内容，请确认！")
        super().clean()


class Picture(models.Model):
    file = models.ImageField(upload_to="")  # 设置文件的上传目录

    def __str__(self):
        """返回名字"""
        return self.file
